//
//  BKODBRResultViewController.m
//  bestkeep
//
//  Created by yons on 17/1/20.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BKODBRResultViewController.h"
#import "BK_OD_Color.h"
#import "Mediator+BKMyOrderDetail.h"
#import "BK_OD_Tool.h"
NSString *const ODBRAccountBalance = @"account/balance";

//余额View
@interface BKODBRAmountView :UIView
- (void)setAmount:(NSString *)amount;
@end

@implementation BKODBRAmountView
{
    UIImageView *_RMBIcon;
    UILabel *_titleLabel;
    UILabel *_amountLabel;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    self.backgroundColor = [UIColor whiteColor];
    
    _RMBIcon = [[UIImageView alloc ]init];
    _RMBIcon.image = [UIImage imageNamed:@"BKOrderRMB_icon"];
    [self addSubview:_RMBIcon];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = UIFontNormal;
    _titleLabel.textColor = kOrderLightGrey1;
    _titleLabel.text = kOrderLocalizeString(@"BKOrderPayAccountAmount");
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_titleLabel];
    
    _amountLabel = [[UILabel alloc] init];
    _amountLabel.font = UIFontLarge;
    _amountLabel.textColor = [UIColor redColor];
    _amountLabel.textAlignment = NSTextAlignmentCenter;
    _amountLabel.text = @"￥0.00";
    [self addSubview:_amountLabel];
    
    [self makeConstraints];
}

- (void)makeConstraints
{
    [_RMBIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self).offset(30);
        make.width.height.mas_equalTo(72);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(_RMBIcon.mas_bottom).offset(15);
        make.left.right.mas_equalTo(self);
    }];
    
    [_amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(_titleLabel.mas_bottom).offset(20);
        make.left.right.mas_equalTo(self);
    }];
}

- (void)setAmount:(NSString *)amount
{
    _amountLabel.text = [NSString stringWithFormat:@"￥%.2f",[amount floatValue]];
}

@end


@interface BKODBRResultViewController ()
@property(nonatomic, strong)BKODBRAmountView *amountView;
@property(nonatomic, strong)UIButton *goOrderListBtn;
@property(nonatomic, strong)ExceptionView *exceptionView;
@end

@implementation BKODBRResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.navigationItem.title = kOrderLocalizeString(@"BKOrderRechargeSuccess");
    
    [self initComponent];
    [self requestAccountBalance];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)initComponent
{
    _amountView = [[BKODBRAmountView alloc] init];
    [self.view addSubview:_amountView];
    
    _goOrderListBtn = ({
        UIButton *btn = [[UIButton alloc] init];
        [btn setTitle:kOrderLocalizeString(@"BKODGoToTheListOfOrdersPaid") forState:UIControlStateNormal]; //去订单列表支付
        btn.titleLabel.font = UIFontLargest;
        btn.layer.cornerRadius = 3;
        btn.layer.masksToBounds = YES;
        UIImage *bgimg = [Common imageWithFrame:CGRectMake(0, 0, 1, 1) color:kOrderBKGreen];
        [btn setBackgroundImage:bgimg forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(goToTheListOfOrdersPaidAction:) forControlEvents:UIControlEventTouchUpInside];
        btn;
    });
    [self.view addSubview:_goOrderListBtn];
    
    [self makeConstraints];
}

- (void)makeConstraints
{
    [_amountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).offset(64);
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(220);
    }];
    
    [_goOrderListBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_amountView.mas_bottom).offset(20);
        make.height.mas_equalTo(48);
        make.left.mas_equalTo(self.view).offset(15);
        make.right.mas_equalTo(self.view).offset(-15);
    }];
}

- (void)requestAccountBalance
{
    __weak typeof(self)weakSelf = self;
    
    if (self.exceptionView) {
        [self.exceptionView dismiss];
        self.exceptionView = nil;
    }
/*
    接口名称：	account_balance
    接口用途：	查询账户信息
    method：	POST
    接口地址：	/account/balance
    输入参数：	无
    返回数据：
    {
        "success": "true/false //成功/失败",
        "msg": "xxx //返回消息",
        "errorCode": "xxx //返回错误编码",
        "data":
        {
            "account":
            [
             {
                 "account_balance": "xxx//账户余额",
                 "account_type": "xxx//账户类型(001 财富账户，002 申购金账户，012 糖罐账户)",
                 "account_type_name": "xxx//账户类型名称 (申购金账户和财富账户)如果是非会员用户直接显示账户余额，且只有申购金账户注意如果是糖罐账户返回的是人民币，但是界面得显示颗，一元=10颗糖",
             }
             ],
            "is_full_member": "0/1 //是否是会员 1:是会员 0：非会员",
        }
    }
*/
    kOrderLoading;
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:kOrderURL(ODBRAccountBalance) parameters:@{} success:^(id  _Nullable responseObject) {
        kOrderDismiss;
        BOOL success = [responseObject[@"success"] boolValue];
        if (!success) {
            kOrderPromptMsg(responseObject[@"msg"]);
            return;
        }
        
        NSArray *accountArr = responseObject[@"data"][@"account"];
        for (NSDictionary *accountDic in accountArr) {
            if ([accountDic[@"account_type"] isEqualToString:@"002"]) {
                
                NSString *amountBlance = accountDic[@"account_balance"];
                [_amountView setAmount:amountBlance];
            }
        }
        
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        kOrderDismiss;
        if (!weakSelf.exceptionView && error) {
            
            ExceptionCodeType codeType;
            if (error.code == -1001) {
                codeType = ExceptionCodeTypeRequestFailed;
            }
            else{
                codeType = ExceptionCodeTypeServerError;
            }
            
            weakSelf.exceptionView = [ExceptionView exceptionViewWithType:codeType];
            ExceptionViewAction *action = [ExceptionViewAction actionWithType:codeType handler:^(ExceptionViewAction *action) {
                [weakSelf requestAccountBalance];
            }];
            [weakSelf.exceptionView addAction:action];
            [weakSelf.exceptionView showInView:weakSelf.view];
        }
    }];
}


//去订单列表
- (void)goToTheListOfOrdersPaidAction:(UIButton *)sender
{
    UIViewController *orderListVC = [[Mediator sharedInstance] GetBKMyOrderViewController];
    [self.navigationController pushViewController:orderListVC animated:YES];
}

@end
